package org.blog.servlets;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.blog.beans.User;
import org.blog.exceptions.InvalidLoginException;
import org.blog.exceptions.InvalidMailException;
import org.blog.exceptions.InvalidPasswordException;
import org.blog.exceptions.LoginAlreadyExistsException;
import org.blog.logic.UserLogic;
import org.blog.messages.RegistrationMessage;

/**
 * Servlet implementation class Main
 */
public class Registration extends HttpServlet {
	private static final long serialVersionUID = 1L;

    /**
     * Default constructor. 
     */
    public Registration() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		action(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		action(request, response);
	}
	
	private void action(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		
		if("register".equals(request.getParameter("do"))) {
		
			User user = new User();
			RegistrationMessage rm = new RegistrationMessage();
		
			try {
				BeanUtils.populate(user, request.getParameterMap());
				
				//nonidentical passwords
				if(!user.getPassword().equals(request.getParameter("password2")))
						rm.setPassword("Hasla musza byc identyczne!");
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			}
			
			try {
				UserLogic.getInstance().register(user);
			} catch (LoginAlreadyExistsException e) {
				rm.setLogin("Login jest juz uzywany!");
			} catch (InvalidLoginException e) {
				rm.setLogin("Niewlasciwy Login!");
			} catch (InvalidPasswordException e) {
				rm.setPassword("Niewlasciwe haslo!");
			} catch (InvalidMailException e) {
				rm.setMail("Niewlasciwy mail!");
			}
			if(rm.error) {
				request.setAttribute("messege", rm);
				request.setAttribute("user", user);
				getServletContext().getRequestDispatcher("/WEB-INF/jsp/registration.jsp").forward(request, response);
			}
			else {
				request.setAttribute("message", "Rejestracja zakonczona powodzeniem");
				getServletContext().getRequestDispatcher("/WEB-INF/jsp/success.jsp").forward(request, response);
			}
			
		}
	
		else
			getServletContext().getRequestDispatcher("/WEB-INF/jsp/registration.jsp").forward(request, response);
	}
}
